 //实现账户密码表单校验
 window.onload = function () {
    //给表单绑定onsubmit事件
    //也就是点击注册按钮就会验证用户名和密码是否正确
    document.getElementById("form").onsubmit = function () {
        //调用用户校验方法  checkUsername();
        //调用密码校验方法  checkPassword();
        return checkUsername() && checkPassword();
    }

    //给用户名和密码分别绑定离焦事件
    //当离焦时就会校验用户名和密码是否正确
    //注意这里的方法没有括号
    document.getElementById("username").onblur = checkUsername;
    document.getElementById("password").onblur = checkPassword;
}

//校验用户名
function checkUsername() {
    //1、获取用户名的值
    var username = document.getElementById("username").value;
    //2、定义正则表达式
    var reg_username = /^\w{6,12}$/;
    //3、判断值是否符合正则的规则
    var flag = reg_username.test(username);
    //4、提示信息
    var s_username = document.getElementById("s_username");

    if (flag) {
        //提示绿色对勾
        s_username.innerHTML = "<img width='35' height='25' src='images/dui.jpg' />";
    } else {
        //提示红色用户名有误
        s_username.innerHTML = "用户名格式有误";
    }
    return flag;
}

//校验密码
function checkPassword() {
    //1、获取用户名的值
    var password = document.getElementById("password").value;
    //2、定义正则表达式
    var reg_password = /^\w{6,12}$/;
    //3、判断值是否符合正则的规则
    var flag = reg_password.test(password);
    //4、提示信息
    var s_password = document.getElementById("s_password");

    if (flag) {
        //提示绿色对勾
        s_password.innerHTML = "<img width='35' height='25' src='images/dui.jpg' />";
    } else {
        //提示红色用户名有误
        s_password.innerHTML = "密码格式有误";
    }
    return flag;
}
// function getCode(n) {
//     var all = "azxcvbnmsdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP0123456789";
//     var b = "";
//     for (var i = 0; i < n; i++) {
//         var index = Math.floor(Math.random() * 62);
//         b += all.charAt(index);

//     }
//     return b;
// };

// function change() {
//     document.getElementById("login").innerHTML = getCode(4);

// }
// window.onload = change;
var show_num = [];
draw(show_num);

function dj() {
    draw(show_num);
}

function sublim() {
    var val = document.getElementById("text").value;
    var num = show_num.join("");
    if (val == '') {
        alert('请输入验证码！');
    } else if (val == num) {
        alert('提交成功！');
        document.getElementById(".input-val").val('');
        draw(show_num);

    } else {
        alert('验证码错误！\n你输入的是:  ' + val + "\n正确的是:  " + num + '\n请重新输入！');
        document.getElementById("text").value = '';
        draw(show_num);
    }



}

function draw(show_num) {
    var canvas_width = document.getElementById('canvas').clientWidth;
    var canvas_height = document.getElementById('canvas').clientHeight;
    var canvas = document.getElementById("canvas"); //获取到canvas的对象，演员
    var context = canvas.getContext("2d"); //获取到canvas画图的环境
    canvas.width = canvas_width;
    canvas.height = canvas_height;
    var sCode =
        "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m";
    var aCode = sCode.split(",");
    var aLength = aCode.length; //获取到数组的长度

    for (var i = 0; i <= 3; i++) {
        var j = Math.floor(Math.random() * aLength); //获取到随机的索引值
        var deg = Math.random() * 30 * Math.PI / 180; //产生0~30之间的随机弧度
        var txt = aCode[j]; //得到随机的一个内容
        show_num[i] = txt;
        var x = 10 + i * 20; //文字在canvas上的x坐标
        var y = 20 + Math.random() * 8; //文字在canvas上的y坐标
        context.font = "bold 23px 微软雅黑";

        context.translate(x, y);
        context.rotate(deg);

        context.fillStyle = randomColor();
        context.fillText(txt, 0, 0);

        context.rotate(-deg);
        context.translate(-x, -y);
    }
    for (var i = 0; i <= 5; i++) { //验证码上显示线条
        context.strokeStyle = randomColor();
        context.beginPath();
        context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
        context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
        context.stroke();
    }
    for (var i = 0; i <= 30; i++) { //验证码上显示小点
        context.strokeStyle = randomColor();
        context.beginPath();
        var x = Math.random() * canvas_width;
        var y = Math.random() * canvas_height;
        context.moveTo(x, y);
        context.lineTo(x + 1, y + 1);
        context.stroke();
    }
}

function randomColor() { //得到随机的颜色值
    var r = Math.floor(Math.random() * 256);
    var g = Math.floor(Math.random() * 256);
    var b = Math.floor(Math.random() * 256);
    return "rgb(" + r + "," + g + "," + b + ")";
}